package com.kfm.jdbc.day0329;

import com.kfm.jdbc.day0328.entity.Account;
import com.mysql.cj.jdbc.MysqlDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;

public class Demo {

    public static void main(String[] args) {
        Properties properties = new Properties();
        try {
            InputStream resourceAsStream = Demo.class.getResourceAsStream("db.properties");
            properties.load(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setURL(properties.getProperty("jdbc.url"));
        dataSource.setUser(properties.getProperty("jdbc.user"));
        dataSource.setPassword(properties.getProperty("jdbc.password"));


        QueryRunner queryRunner = new QueryRunner(dataSource);
        String sql = "select * from account";
        try {
            List<Account> account = queryRunner.query(sql, new BeanListHandler<>(Account.class));
            System.out.println(account);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
}
